package com.restaurant.application.indexes.reservationlist;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.nebutown.base.ViewSpec;
import com.nebutown.base.data.View;
import com.nebutown.base.data.ViewUpdate;
import com.nebutown.commonunits.dataface.*;
import com.nebutown.commonunits.dataface.definitions.*;
import com.nebutown.commonunits.uidataface.UIDataFace;
import com.nebutown.commonunits.utils.DateTimeUtils;
import com.nebutown.generation.basictype.UID;
import com.restaurant.application.indexes.reservationlist.mysql.ReservationListBean;
import com.restaurant.application.indexes.reservationlist.mysql.ReservationListCriteria;
import com.restaurant.application.indexes.reservationlist.mysql.ReservationListImpl;
import com.restaurant.application.indexes.reservationlist.support.AbstractAssistReservationList;
import com.restaurant.application.minifacade.data.ReservationPageBO;
import com.restaurant.application.reservation.Reservation;
import com.restaurant.application.reservation.support.ReservationData;
import com.restaurant.application.util.CommonUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author Administrator
 */
public class AssistReservationListImpl extends AbstractAssistReservationList {
    @Override
    public View getFullView() {
        return newCommonTable(new ReservationListCriteria());
    }

    @Override
    public View getViewLead(ViewSpec viewSpec) {
        return null;
    }

    @Override
    public ViewUpdate search(ReservationListCriteria search, int[] seq) {
        return returnView(newCommonTable(search));
    }

    @Override
    public ViewUpdate modify(int[] seq) {
        List<ReservationListBean> beans = getIndex(ReservationListImpl.class).search(new ReservationListCriteria().withRecordId((long) seq[0]), null, 1);
        if (beans.size() > 0) {
            return returnPushInContainer(beans.get(0).getReservationUid(), false);
        }
        return returnView(getFullView());
    }

    @Override
    public List<ReservationPageBO> getReservationBean(UID customerUid) {
        List<ReservationListBean> beans = getIndex(ReservationListImpl.class)
                .search(new ReservationListCriteria().withCustomerUidEquals(customerUid.value()), CommonUtils.getSort(), 20);
        List<ReservationPageBO> beanList = Lists.newArrayList();
        if (beans.size() > 0) {
            beans.forEach(e -> beanList.add(getReservationPageBean(e)));
        }
        return beanList;
    }

    private ReservationPageBO getReservationPageBean(ReservationListBean bean) {
        return new ReservationPageBO()
                .withOrderId(bean.getNumber())
                .withCreateTime(bean.getCreateTime())
                .withShopDate(CommonUtils.longToTimeString(bean.getDate()))
                .withShopTime(bean.getTime())
                .withStatus(bean.getState())
                .withShopName(bean.getShopName())
                .withCount(Integer.parseInt(bean.getPersonAmount()))
                .withRemark(bean.getRemarks())
                .withService(bean.getServiceName());
    }

    private View newCommonTable(ReservationListCriteria criteria) {
        List<ReservationListBean> beans = getIndex(ReservationListImpl.class).search(criteria, CommonUtils.getSort(), 0, 20);
        int totalRecord = getIndex(ReservationListImpl.class).count(criteria);
        PageInfo info = CommonUtils.getPageInfo(1, 20, totalRecord);
        PageForm pageForm = newFormShell(criteria, beans, info).getForm();
        return getSingleton(UIDataFace.class).newGenericForm(unitPosition(), pageForm);
    }

    private FormShell newFormShell(ReservationListCriteria criteria, List<ReservationListBean> beans, PageInfo info) {
        FormShell shell = new FormShell("预约/预约订单", 3, 2);

        shell.source(criteria, DataModes.EDIT);
        shell.at(0, 0, "手机号")
                .getEntry(ReservationListCriteria.FN_PHONELIKE, criteria.getPhoneLike())
                .placeHolder("手机号")
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(true);
        shell.at(0, 1, "昵称")
                .getEntry(ReservationListCriteria.FN_CUSTOMERNAMELIKE, criteria.getCustomerNameLike())
                .placeHolder("昵称")
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(true);
        shell.at(0, 2, "会员编号")
                .getEntry(ReservationListCriteria.FN_CUSTOMERIDLIKE, criteria.getCustomerIdLike())
                .placeHolder("会员编号")
                .simpleEntry(EntryTypes.STRING)
                .getContent().setEditable(true);
        shell.at(1, 0, "预约时间")
                .getEntry(ReservationListCriteria.FN_CREATETIMEUPPER, String.valueOf(criteria.getCreateTimeUpper()))
                .placeHolder("预约时间")
                .date(DateTypes.DATETIME, "yyyy-MM-dd")
                .getContent().setEditable(true);
        shell.at(1, 1, "-")
                .getEntry(ReservationListCriteria.FN_CREATETIMELOWER, String.valueOf(criteria.getCreateTimeLower()))
                .placeHolder("-")
                .date(DateTypes.DATETIME, "yyyy-MM-dd")
                .getContent().setEditable(true);
        shell.at(1, 2, "预约状态")
                .getEntry(ReservationListCriteria.FN_STATEEQUALS, criteria.getStateEquals())
                .placeHolder("预约状态")
                .selectFrom(Reservation.STATE_ACCEPT, Reservation.STATE_CANCEL, Reservation.STATE_START, Reservation.STATE_REFUSE)
                .getContent().setEditable(true);

        shell.at(2, 1, "查询")
                .buttonField(getPath(CMD_SEARCH), "", true);

        TableShell tableShell = new TableShell<>(ReservationListBean.class, beans).withPageInfo(info);
        tableShell.createColumn(ReservationListBean.FN_CREATETIME).withName("时间").cellCalculator(e -> {
            ReservationListBean bean = (ReservationListBean) e;
            return new CellData().withField(new FieldContent().withSimpleField(DateTimeUtils.toTime(bean.getCreateTime())));
        });
        tableShell.createColumn(ReservationListBean.FN_NUMBER).withName("预约单").cellCalculator(e -> {
            ReservationListBean bean = (ReservationListBean) e;
            return new CellData().withField(new FieldContent().withSimpleField(bean.getNumber()));
        });
        tableShell.createColumn(ReservationListBean.FN_CUSTOMERID).withName("会员ID").cellCalculator(e -> {
            ReservationListBean bean = (ReservationListBean) e;
            return new CellData().withField(new FieldContent().withSimpleField(bean.getCustomerId()));
        });
        tableShell.createColumn(ReservationListBean.FN_CUSTOMERNAME).withName("会员昵称").cellCalculator(e -> {
            ReservationListBean bean = (ReservationListBean) e;
            return new CellData().withField(new FieldContent().withSimpleField(bean.getCustomerName()));
        });
        tableShell.createColumn(ReservationListBean.FN_PHONE).withName("手机号").cellCalculator(e -> {
            ReservationListBean bean = (ReservationListBean) e;
            return new CellData().withField(new FieldContent().withSimpleField(bean.getPhone()));
        });
        tableShell.createColumn(ReservationListBean.FN_SHOPNAME).withName("预约门店").cellCalculator(e -> {
            ReservationListBean bean = (ReservationListBean) e;
            return new CellData().withField(new FieldContent().withSimpleField(bean.getShopName()));
        });
        tableShell.createColumn(ReservationListBean.FN_TIME).withName("预约时间段").cellCalculator(e -> {
            ReservationListBean bean = (ReservationListBean) e;
            return new CellData().withField(new FieldContent().withSimpleField(bean.getTime()));
        });
        tableShell.createColumn(ReservationListBean.FN_PERSONAMOUNT).withName("人数").cellCalculator(e -> {
            ReservationListBean bean = (ReservationListBean) e;
            return new CellData().withField(new FieldContent().withSimpleField(bean.getPersonAmount()));
        });
        tableShell.createColumn(ReservationListBean.FN_SERVICENAME).withName("服务").cellCalculator(e -> {
            ReservationListBean bean = (ReservationListBean) e;
            return new CellData().withField(new FieldContent().withSimpleField(bean.getServiceName()));
        });
        tableShell.createColumn(ReservationListBean.FN_STATE).withName("状态").cellCalculator(e -> {
            ReservationListBean bean = (ReservationListBean) e;
            return new CellData().withField(new FieldContent().withSimpleField(bean.getState()));
        });
        tableShell.createColumn("编辑").withName("编辑").cellCalculator(e -> {
            ReservationListBean bean = (ReservationListBean) e;
            return new CellData().withField(new FieldContent().withButton(new ButtonField().withLabel("编辑").withButtonPath(getPath(CMD_MODIFY, bean.getRecordId()))));
        });

        shell.at(3, 0, "预约/预约订单")
                .large(3, 1)
                .getEntry("error", "")
                .tableEntry(tableShell);
        return shell;
    }

    @Override
    public ReservationListBean createBean(UID uid, ReservationData data) {
        if (Objects.isNull(uid) || Objects.isNull(data)) {
            return null;
        }
        return new ReservationListBean()
                .withReservationUid(uid)
                .withId(data.getId())
                .withCreateTime(data.getCreateTime())
                .withModifyTime(data.getModifyTime())
                .withNumber(data.getNumber())
                .withShopUid(data.getShopUid().value())
                .withShopName(data.getShopName())
                .withServiceName(data.getServiceName())
                .withPersonAmount(String.valueOf(data.getPersonAmount()))
                .withDate(data.getDate())
                .withTime(data.getTime())
                .withCustomerId(data.getCustomerId())
                .withCustomerUid(data.getCustomerUid().value())
                .withCustomerName(data.getCustomerName())
                .withPhone(data.getPhone())
                .withRemarks(data.getRemarks())
                .withState(data.getState());
    }

    @Override
    public ReservationListBean updateBean(UID uid, ReservationData data, ReservationListBean bean) {
        if (Objects.isNull(uid) || Objects.isNull(data) || Objects.isNull(bean)) {
            return null;
        } else {
            bean.withReservationUid(uid);
        }
        if (Objects.nonNull(data.getModifyTime())) {
            bean.withModifyTime(data.getModifyTime());
        }
        if (Objects.nonNull(data.getShopUid())) {
            bean.withShopUid(data.getShopUid().value()).withShopName(data.getShopName());
        }
        if (!Strings.isNullOrEmpty(data.getServiceName())) {
            bean.withServiceName(data.getServiceName());
        }
        if (Objects.nonNull(data.getPersonAmount()) && data.getPersonAmount() > 0) {
            bean.withPersonAmount(String.valueOf(data.getPersonAmount()));
        }
        if (Objects.nonNull(data.getDate())) {
            bean.withDate(data.getDate());
        }
        if (Objects.nonNull(data.getTime())) {
            bean.withTime(data.getTime());
        }
        if (!Strings.isNullOrEmpty(data.getPhone())) {
            bean.withPhone(data.getPhone());
        }
        if (!Strings.isNullOrEmpty(data.getState())) {
            bean.withState(data.getState());
        }
        if (!Strings.isNullOrEmpty(data.getRemarks())) {
            bean.withRemarks(data.getRemarks());
        }
        return bean;
    }
}
